/** 
 * File: Location.java
 * Project: CMSC 345 Course Registration System, Fall 2010
 * Author: David Chong
 * Date: 12/5/10
 * E-mail: ro62899@umbc.edu
 * This class is used to represent a Location for a Course.  There are mutators
 * and accessor for the private data fields.  In addition there is a toString
 * method to display the objects state. 
 * 
 * 
 * Class Invariant: None
 */

package ocrs.server.registeringSystem;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class Location 
{
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key theKey;
	
	@Persistent
	private String campusSite;
	
	@Persistent
	private String buildingName;
	
	@Persistent
	private int roomNumber;
	
	
	/** 
     * Name: Location <br><br>
     * The constructor that initializes its private data members.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Initializes its private data members.
     * <br>
     * @param None
     */
	public Location()
	{
		campusSite = "";
		buildingName = "";
		roomNumber = 0;
	} // end of default constructor
	
	
	
	/** 
     * Name: Location <br><br>
     * The constructor that accepts parameters and initializes the private data
     * with arguments passed in.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Initializes the private data members with the arguments
     *                passed in.
     * <br>
     * @param campusSite - the campus site
     * @param buildingName - the building's name
     * @param roomNumber - the room number
     * @throws NullReferenceException is a checked exception that is thrown
     *         when a null reference is passed into the campusSite or 
     *         buildingName.
     * @throws NotValidArgumentException is a checked exception that is thrown
     *         when a negative number is passed into the room number
     */
	public Location(String campusSite, String buildingName, int roomNumber)
	                                          throws NotValidArgumentException,
	                                                     NullReferenceException
	{
		if(campusSite == null || buildingName == null)
			throw new NullReferenceException("Did not create the Location " +
					                         "object because a null " +
					                         "reference was passed in!\n");
			
		else if(roomNumber < 0)
			throw new NotValidArgumentException("Did not create the Location" +
					                            "object because a negative " +
					                            "number was passed in!\n");	
		
		this.campusSite = campusSite;
		this.buildingName = buildingName;
		this.roomNumber = roomNumber;
	} // end of constructor
	
	
	
	/** 
     * Name: Location <br><br>
     * The constructor that performs a deep copy of the object passed in.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Initializes the private data members with the object 
     *                passed in. <br>
     * @param copyThis - the object that will be copied to initialize the
     *                   calling object's private data members
     * @throws NullReferenceException is a checked exception that is thrown
     *         when a null reference is passed into the campusSite or 
     *         buildingName.
     */
	public Location(Location copyThis) throws NullReferenceException
	{
		if(copyThis == null)
			throw new NullReferenceException("Did not create the Location " +
					                         "object because a null " +
					                         "reference was passed in!\n");	
		
		campusSite = copyThis.campusSite;
		buildingName = copyThis.buildingName;
		roomNumber = copyThis.roomNumber;
	} // end of constructor
	
	
	
	/** 
     * Name: getTheKey <br><br>
     * An accessor method that retrieves the key for the object in the Google
     * datastore.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns the key. <br>
     * @param None
     */
	public Key getTheKey()
	{
		return theKey;
	} // end of getTheKey()
	
	
	
	/** 
     * Name: getCampusSite <br><br>
     * An accessor method that retrieves the campus site.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns the campusSite. <br>
     * @param None
     */
	public String getCampusSite()
	{
		return campusSite;
	} // end of getCampusSite()
	
	
	
	/** 
     * Name: getBuildingName <br><br>
     * An accessor method that retrieves the building's name.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns the building's name. <br>
     * @param None
     */
	public String getBuildingName()
	{
		return buildingName;
	} // end of getBuildingName()
	
	
	
	/** 
     * Name: getRoomNumber <br><br>
     * An accessor method that retrieves the room number.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns the room number. <br>
     * @param None
     */
	public int getRoomNumber()
	{
		return roomNumber;
	} // end of getRoomNumber()
	
	
	
	/** 
     * Name: setCampusSite <br><br>
     * A mutator to change the campusSite field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Changes the campusSite to the value passed in.
     * <br>
     * @param campusSite - the argument that will be used to set the field
     * @throws NullReferenceException is a checked exception that is thrown
     *         when a null reference is passed in.
     */
	public void setCampusSite(String campusSite) throws NullReferenceException
	{
		if(campusSite == null)
			throw new NullReferenceException("The campus site was not set " +
					                          "because a null reference was " +
					                          "passed in!\n");
		
			this.campusSite = campusSite;
	} // end of setCampusSite()
	
	
	
	/** 
     * Name: setBuildingName <br><br>
     * A mutator to change the buildingName field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Changes the buildingName to the value passed in.
     * <br>
     * @param buildingName - the argument that will be used to set the field
     * @throws NullReferenceException is a checked exception that is thrown
     *         when a null reference is passed in.
     */
	public void setBuildingName(String buildingName) 
	                                              throws NullReferenceException
	{
		if(buildingName == null)
			throw new NullReferenceException("The building name was not set " +
					                          "because a null reference was " +
					                          "passed in!\n");

        this.buildingName = buildingName;
	} // end of setBuildingName
	
	
	
	/** 
     * Name: setRoomNumber <br><br>
     * A mutator to change the roomNumber field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Changes the roomNumber to the value passed in.
     * <br>
     * @param roomNumber - the argument that will be used to set the field
     * @throws NotValidArgumentException is a checked exception that is thrown
     *         when a negative number is passed in.
     */
	public void setRoomNumber(int roomNumber) throws NotValidArgumentException
	{
		if(roomNumber < 0)
			throw new NotValidArgumentException("The room number was not set " +
					                            "because a negative number " +
					                            "was passed in!\n");
		
		this.roomNumber = roomNumber;
	} // end of setRoomNumber
	
	
	
	/** 
     * Name: toString <br><br>
     * Displays the object's state.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns a String representing the object's state.
     * <br>
     * @param None
     */
	public String toString()
	{
		return "Campus Site:   " + campusSite + "\n" +
		       "Building Name: " + buildingName + "\n" +
		       "Room Number:   " + roomNumber + "\n\n";
	} // end of toString()
	
	
	
	/** 
     * Name: equals <br><br>
     * Compares two object and determines if the objects are equal.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns a boolean that determines if the objects are
     *                equal.
     * <br>
     * @param otherLocation - the other object that will be compared
     *                        to the calling object
     */
	public boolean equals(Location otherLocation)
	{
		boolean areTheyEqual;
		
		if(otherLocation == null)
			areTheyEqual = false;
		
		else
			areTheyEqual = campusSite.equals(otherLocation.campusSite) &&
			               buildingName.equals(otherLocation.buildingName) &&
			               roomNumber == otherLocation.roomNumber;
		
		return areTheyEqual;
	} // end of equals()
} // end of Location class